// import axios from 'axios'
import request from '@/utils/request'

export default {
  namespaced: true,
  state: {
    // curChannelId: 0, // 当前频道id，默认取0不合适，因为后台接口返回的频道列表第一个频道不一定就是0
    curChannelId: null, // 当前频道id
    list: [],
  },
  getters: {},
  mutations: {
    setCurChannelId(state, payload) {
      state.curChannelId = payload // 当前频道id改变
      // 频道id只要变了，文章列表就要变
      this.dispatch('newlist/getList')
    },
    setList(state, payload) {
      state.list = payload
    },
  },
  actions: {
    async getList(ctx) {
      const res = await request({
        method: 'GET',
        url: '/v1_0/channels',
      })
      // console.log(res, 'res')
      const { channels } = res.data.data
      // const channels = res.data.data.channels
      ctx.commit('setList', channels)
      if (channels.length) {
        ctx.commit('setCurChannelId', channels[0].id)
      }
    },
  },
}
